package com.autorun.server.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.autorun.server.entity.Apk;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * APK Mapper接口
 * 
 * @author autorun
 * @version 1.0.0
 */
@Mapper
public interface ApkMapper extends BaseMapper<Apk> {

    /**
     * 检查版本号是否重复
     * 
     * @param game 游戏名
     * @param versionCode 版本代码
     * @return 数量
     */
    @Select("SELECT COUNT(*) FROM apks WHERE game = #{game} AND version_code = #{versionCode}")
    Integer countByGameAndVersionCode(@Param("game") String game, @Param("versionCode") Integer versionCode);

    /**
     * 获取最新的激活APK
     * 
     * @param game 游戏名
     * @return APK信息
     */
    @Select("SELECT * FROM apks WHERE game = #{game} AND is_active = true ORDER BY version_code DESC LIMIT 1")
    Apk findLatestByGame(@Param("game") String game);

    /**
     * 获取最新的激活APK（所有游戏）
     * 
     * @return APK信息
     */
    @Select("SELECT * FROM apks WHERE is_active = true ORDER BY upload_time DESC LIMIT 1")
    Apk findLatest();
}